// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   Authenticator.java

package org.quickserver.net.qsadmin;

import java.io.IOException;
import java.util.Arrays;
import org.quickserver.net.AppException;
import org.quickserver.net.server.*;

// Referenced classes of package org.quickserver.net.qsadmin:
//            Data

public class Authenticator extends QuickAuthenticationHandler
{

    public Authenticator()
    {
    }

    public AuthStatus askAuthentication(ClientHandler handler)
        throws IOException, AppException
    {
        Data data = (Data)handler.getClientData();
        data.setLastAsked("U");
        handler.sendClientMsg("+OK Username required");
        return null;
    }

    public AuthStatus handleAuthentication(ClientHandler handler, String command)
        throws IOException, AppException
    {
        Data data = (Data)handler.getClientData();
        if(data.getLastAsked().equals("U"))
        {
            data.setUsername(command);
            data.setLastAsked("P");
            handler.sendClientMsg("+OK Password required");
        } else
        if(data.getLastAsked().equals("P"))
        {
            data.setPassword(command.getBytes());
            if(validate(data.getUsername(), data.getPassword()))
            {
                handler.sendClientMsg("+OK Logged in");
                data.setPassword(null);
                return AuthStatus.SUCCESS;
            } else
            {
                handler.sendClientMsg("-ERR Authorisation Failed");
                data.setPassword(null);
                return AuthStatus.FAILURE;
            }
        } else
        {
            throw new AppException("Unknown LastAsked!");
        }
        return null;
    }

    protected static boolean validate(String username, byte password[])
    {
        return username.equals("Admin") && Arrays.equals(password, "QsAdm1n".getBytes());
    }
}
